from flask import Flask, cli, render_template, request
from commands import CryptoProCommand
import rsa, base64, paramiko
app = Flask(__name__)

@app.route('/')
def index():
    return render_template("index.html")

@app.route('/send', methods=["POST"])
def send():
    hostname = request.form.get('hostname')
    password = request.form.get('ssh-password', 'test')

    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy)

    with open('private.key', 'rb') as keyfile:
        key = rsa.PrivateKey.load_pkcs1(keyfile.read(), 'PEM')
        password = rsa.decrypt(base64.b64decode(password), key).decode()
        client.connect(hostname=hostname, username='root', password=password)

    command = CryptoProCommand(client)
    result = command.execute()
    client.close()
    return result

@app.route('/publickey', methods=["GET"])
def publickey():
    with open('public.pem', 'r') as pkey:
        return pkey.read()


app.run(debug=True)